//配置路由相关的信息
//1.导入路由
import VueRouter from "vue-router";
import Vue from "vue";

// import Home from '../components/Home.vue'
// import About from '../components/About.vue'
// import User from '../components/User.vue'

/**
 * 解决 Home.vue 里重复触发了 home/news 路由问题
 */
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
const Home = () => import("../components/Home.vue");
const About = () => import("../components/About.vue");
const User = () => import("../components/User.vue");
const HomeNews = () => import("../components/HomeNews.vue");
const HomeMessages = () => import("../components/HomeMessages.vue");
const Profile = () => import("../components/Profile.vue");

//2.通过 Vue.use(插件名称),安装插件
Vue.use(VueRouter);

//配置路由和组件之间的映射关系
const routes = [
  {
    //配置默认加载路由
    path: "/",
    redirect: "/home" //重定向
  },
  {
    path: "/home",
    component: Home,
    children: [
      // {
      //     path: '/',
      //     redirect: '/home/news'
      // },
      {
        path: "news",
        component: HomeNews
      },
      {
        path: "messages",
        component: HomeMessages
      }
    ]
  },
  {
    path: "/about",
    component: About
  },
  {
    path: "/user/:userId",
    component: User
  },
  {
    path: "/profile",
    component: Profile
  }
];

//创建 VueRouter 对象
const router = new VueRouter({
  routes,
  mode: "history", //这个配置只是美化url地址用的,去除url中的#号
  linkActiveClass: "active"
});

//3.将 router 对象导出
export default router;
